﻿@using System.Data.Entity;
@using eLoan.Models;
@model LoanAddEditModel

@{
    ViewBag.Title = "Add/Edit Loan";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Add/Edit Loan</h2>
        <ol class="breadcrumb">
            <li>
                Funding
            </li>
            <li>
                <a href="@Url.Action("Loans", "Funding")">Loans</a>
            </li>
            <li class="active">
                <strong>Add/Edit Loan</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div id="row" class="row">
        <div class="col-lg-6">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("Save", "Loan", FormMethod.Post, new { id = "saveForm" }))
            {
                <div id="divValidations" class="ibox-title" style="display:none">
                    <div id="valDocumentNo" style="display:none"><span>Document number is required.</span></div>
                    <div id="valDocumentDate" style="display:none"><span>Invalid document date.</span></div>
                    <div id="valBranch" style="display:none"><span>Branch is required.</span></div>
                    <div id="valAgent" style="display:none"><span>Agent is required.</span></div>
                    <div id="valBorrower" style="display:none"><span>Borrower is required.</span></div>
                    <div id="valLoanAmount" style="display:none"><span>Loan amount must be greater than 0.00.</span></div>
                    <div id="valCredit" style="display:none"><span>Loan amount must not exceed credit limit.</span></div>
                    <div id="valLoanInterest" style="display:none"><span>Loan interest must be greater than 0.00.</span></div>
                    <div id="valProceedType" style="display:none"><span>Proceed type is required.</span></div>
                    <div id="valTerm" style="display:none"><span>Division of proceeds must be greater than 0.</span></div>
                    @*<div id="valPaymentStartDate" style="display:none"><span>Invalid payment start date.</span></div>*@
                    @*<div id="valSecondDate" style="display:none"><span>Second date for semi-monthly payment frequency is required.</span></div>*@
                    @*<div id="valPaymentEndDate" style="display:none"><span>Invalid payment end date.</span></div>*@
                </div>
                <div class="ibox-content">

                    
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <fieldset>

                            <table class="table table-bordered table-striped">
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.DocumentNo)</th>
                                    <td>
                                        @Html.HiddenFor(m => m.LoanModel.Loan.DocumentNo, new { @id = "hdnDocumentNo" })
                                        <span id="spnDocumentNo">@Model.LoanModel.Loan.DocumentNo</span>
                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.DocumentDate)</th>
                                    <td>
                                        <div class="form-group" id="dd">
                                            <div class="input-group date">
                                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                                @Html.TextBoxFor(m => m.LoanModel.Loan.DocumentDate, "{0:MM/dd/yyyy}", new { @id = "txtDocumentDate", @class = "form-control" })
                                            </div>
                                        </div>
                                        @*@Html.ValueFor(m => m.LoanModel.Loan.DocumentDate, "{0:MMM/dd/yyyy}")
                                        @Html.HiddenFor(m => m.LoanModel.Loan.DocumentDate)*@
                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.BranchCode)</th>
                                    <td>
                                        @Html.DropDownListFor(m => m.LoanModel.Loan.BranchCode, Model.Branches, new { @id = "ddlBranches", @class = "text-box single-line", @onchange = "ChangeAgents();SearchBorrowerList();GetNextDocumentNo();" })
                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.AgentID)</th>
                                    <td>@Html.DropDownListFor(m => m.LoanModel.Loan.AgentID, Model.Agents, new { @id = "ddlAgents", @class = "text-box single-line" })</td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.BorrowerID)</th>
                                    <td>
                                        @Html.HiddenFor(m => m.LoanModel.Loan.BorrowerID, new { @id = "hdnBorrowerID" })
                                        <span id="spanBorrowerName">@Model.LoanModel.BorrowerName</span>
                                        <span class="pull-right"><a id="lnkSearchBorrower" onclick="SearchBorrower();">Search</a></span>
                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.CreditLimit)</th>
                                    <td>
                                        @Html.HiddenFor(m => m.LoanModel.Loan.CreditLimit, new { @id = "hdnCreditLimit" })
                                        <span id="spnCreditLimit">@Html.ValueFor(m => m.LoanModel.Loan.CreditLimit, "{0:#,###,##0.00}")</span>
                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.LoanAmount)</th>
                                    <td>@Html.TextBoxFor(m => m.LoanModel.Loan.LoanAmount, "{0:#,###,##0.00}", new { @id = "txtLoanAmount", @class = "text-box single-line", @onchange = "FormatMe(this); ValidateLoanAmount();" })</td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.LoanInterest)</th>
                                    <td>@Html.TextBoxFor(m => m.LoanModel.Loan.LoanInterest, "{0:#,###,##0.00}", new { @id = "txtLoanInterest", @class = "text-box single-line", @onchange = "FormatMe(this); ValidateLoanInterest();" })</td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.LoanTypeID)</th>
                                    <td>@Html.DropDownListFor(m => m.LoanModel.Loan.LoanTypeID, Model.LoanTypes, new { @id = "ddlLoanType", @class = "text-box single-line" })</td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.PaymentTypeID)</th>
                                    <td>@Html.DropDownListFor(m => m.LoanModel.Loan.PaymentTypeID, Model.PaymentTypes, new { @id = "ddlPaymentType", @class = "text-box single-line" })</td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.Term)</th>
                                    <td>@Html.TextBoxFor(m => m.LoanModel.Loan.Term, new { @id = "txtTerm", @class = "text-box single-line" })</td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.ProceedTypeID)</th>
                                    <td>@Html.DropDownListFor(m => m.LoanModel.Loan.ProceedTypeID, Model.ProceedTypes, new { @id = "ddlProceedType", @class = "text-box single-line" })</td>
                                </tr>
                                @*<tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.PaymentStartDate)</th>
                                    <td>
                                        <div class="form-group" id="psd">
                                            <div class="input-group date">
                                                <span class="input-group-addon">
                                                    <i class="fa fa-calendar"></i>
                                                </span>
                                                @Html.TextBoxFor(m => m.LoanModel.Loan.PaymentStartDate, "{0:MM/dd/yyyy}", new { @id = "txtPaymentStartDate", @class = "form-control" })
                                            </div>
                                        </div>
                                    </td>
                                </tr>*@
                                @*<tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.PaymentEndDate)</th>
                                    <td>
                                        <div class="form-group" id="ped" >
                                            <div class="input-group date">
                                                <span class="input-group-addon">
                                                    <i class="fa fa-calendar"></i>
                                                </span>
                                                @Html.TextBoxFor(m => m.LoanModel.Loan.PaymentEndDate, "{0:MM/dd/yyyy}", new { @id = "txtPaymentEndDate", @class = "form-control", @readonly ="readonly" })
                                            </div>
                                        </div>
                                    </td>
                                </tr>*@
                                @*<tr id="trSecondDate">
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.PaymentSecondDate)</th>
                                    <td>@Html.TextBoxFor(m => m.LoanModel.Loan.PaymentSecondDate, new { @id = "txtSecondDate", @class = "single-line" })</td>
                                </tr>*@
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.EncodedBy)</th>
                                    <td>
                                        @Html.HiddenFor(m => m.LoanModel.Loan.EncodedBy)
                                        @Html.DisplayFor(m => m.LoanModel.Loan.EncodedBy)
                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.StatusID)</th>
                                    <td>
                                        @Html.HiddenFor(m => m.LoanModel.Loan.StatusID)
                                        @Html.DisplayFor(m => m.LoanModel.LoanStatus)
                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.LoanModel.Loan.IsActive)</th>
                                    <td>@Html.CheckBoxFor(m => m.LoanModel.Loan.IsActive, new { @checked = "checked" })</td>
                                </tr>
                            </table>

                            <p></p>

                            <button type="button" id="btnValidate" class="btn btn-primary" onclick="ValidateFields();" >Save</button>
                            <button type="submit" id="btnCancel" class="btn btn-primary cancel" name="command" value="Cancel">Cancel</button>
                            <button type="submit" id="btnSubmit" style="display:none" name="command" value=@Model.DBCommand>Cancel</button>
                        </fieldset>
                </div>
            }
            </div>

        </div>

        <div id="divSearchBorrower" class="col-lg-6" style="display:none;">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <div class="pull-right" style="padding:15px 15px 15px 15px;">
                        <a id="lnkCloseSearchBorrower" href="javascript:;">Close</a>
                    </div>
                    <div class="divSearch">
                        <table class="table-search">
                            <tr>
                                <td>
                                    <span><strong>Search Text</strong></span><p></p>
                                    <input type="text" id="txtSearchText" class="text-box single-line" />
                                </td>
                                <td>
                                    <button style="margin-bottom:0px" type="button" id="btnSearchBorrower" class="btn btn-primary" onclick="SearchBorrowerList()">Search</button>
                                </td>
                                <td>
                                    <input id="chkExcludeWithActiveLoans" type="checkbox" checked="checked" />
                                    <span>Exclude with active loans</span>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="ibox-content">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th><span>Borrower</span></th>
                                <th><span>Agent</span></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody id="bodyBorrowers">
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
    </div>
</div>

@section Styles {
    @Styles.Render("~/plugins/dataPickerStyles")

    <style>
        .table-search {
            width: 90%;
            border: none;
        }

            .table-search td {
                margin: 10px 10px 10px 10px;
                padding: 10px 10px 10px 10px;
                vertical-align: bottom;
            }

        .divSearch {
            background-color: #F5F5F6;
            border: 1px solid #EBEBEB;
        }
    </style>

}

@section Scripts {
    @*@Scripts.Render("~/bundles/jqueryval")*@
    @Scripts.Render("~/plugins/dataPicker")

<script id="onDocumentReady" type="text/javascript">
    $(document).ready(function () {

        $('#dd .input-group.date').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            format: 'mm/dd/yyyy',
            autoclose: true

        });


        //$('#psd .input-group.date').datepicker({
        //    todayBtn: "linked",
        //    keyboardNavigation: false,
        //    forceParse: false,
        //    calendarWeeks: true,
        //    format: 'mm/dd/yyyy',
        //    autoclose: true,
        //    daysOfWeekDisabled: [0]

        //});

        //ShowSecondDate($('#ddlProceedType'));

        //$('#ped .input-group.date').datepicker({
        //    todayBtn: "linked",
        //    keyboardNavigation: false,
        //    forceParse: false,
        //    calendarWeeks: true,
        //    format: 'mm/dd/yyyy',
        //    autoclose: true

        //});

        
        //$('#psd .input-group.date').datepicker()
        //    .on('changeDate', function (e) {
        //        SetPaymentEndDate();
        //    });

        //$('#ped .input-group.date').datepicker()
        //    .on('changeDate', function (e) {
        //        SetPaymentEndDate();
        //    });

        $('').attr('disabled', true);

        $('#lnkCloseSearchBorrower').on('click', function () {
            $('#divSearchBorrower').fadeOut(200);
            });

        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                return false;
            }
        });
    });

</script>

<script id="methods" type="text/javascript">
    function CheckTerm() {
        var div = $('#txtTerm').val().trim();

        if (div == '' || isNaN(div)) {
            $('#txtTerm').val('1');
        }
    }

    //function CheckPaymentStartDate() {

    //    var sd = $('#psd .input-group.date').datepicker('getDate');
    //    var newDate = new Date();

    //    if (isNaN(Date.parse(sd))) {
    //        $('#psd .input-group.date').datepicker("update", newDate);
    //    }
        
    //    //if (Date.parse(sd) < newDate) {
    //    //    $('#psd .input-group.date').datepicker("update", newDate);
    //    //}
    //}

    //function SetPaymentDays()
    //{
    //    CheckTerm();
    //    CheckPaymentStartDate();

    //    var term = $('#txtTerm').val().trim();
    //    var sd = $('#psd .input-group.date').datepicker('getDate');
    //    var date = new Date(sd);
        
    //    date.setDate(date.getDate() + parseInt(term) - 1);

    //    //$('#ped .input-group.date').datepicker("update", new Date(date));
    //}

    //function SetPaymentEndDate() {
    //    CheckTerm();
    //    CheckPaymentStartDate();

    //    var div = $('#txtTerm').val().trim();
    //    var sd = $('#psd .input-group.date').datepicker('getDate');
    //    var date = new Date(sd);

    //    var proceedType = $('#ddlProceedType').val();

    //    switch (proceedType) {
    //        case "1": // daily
    //            {
    //                date.setDate(date.getDate() + parseInt(div) - 1);
    //                break;
    //            }
    //        case "2": // weekly
    //            {
    //                date.setDate(date.getDate() + ((parseInt(div) - 1) * 7));
    //                break;
    //            }
    //        case "3": // semi-monthly
    //            {
    //                date.setDate(date.getDate() + ((parseInt(div) - 1) * 15));
    //                break;
    //            }
    //        case "4": // monthly
    //            {
    //                date.setMonth(date.getMonth() + (parseInt(div) - 1));
    //                break;
    //            }
    //        case "5": // biMonthly
    //            {
    //                date.setMonth(date.getMonth() + (parseInt(div) - 1) * 2);
    //                break;
    //            }
    //    }

    //    //$('#ped .input-group.date').datepicker("update", new Date(date));
    //}

    //function ShowSecondDate(me)
    //{
    //    var frequency = $(me).val();

    //    if (frequency == 3)
    //        $('#trSecondDate').show();
    //    else
    //        $('#trSecondDate').hide();

    //}

    //function ValidatePaymentEndDateValue() {
    //    var sd = $('#psd .input-group.date').datepicker('getDate');
    //    var ed = $('#ped .input-group.date').datepicker('getDate');

    //    if (ed < sd) {
    //        $('#ped .input-group.date').datepicker("update", new Date(sd));
    //    }
    //}

    function SearchBorrower() {
        $('#divSearchBorrower').fadeIn(200);
    }

    function SelectBorrower(borrowerID, borrowerName, agentID, creditLimit) {
        $('#hdnBorrowerID').val(borrowerID);
        $('#spanBorrowerName').text(borrowerName);
        $('#ddlAgents').val(agentID);

        var formatted = FormatNumber(parseFloat(creditLimit).toFixed(2));
        $('#hdnCreditLimit').val(formatted);
        $('#spnCreditLimit').text(formatted);

        $('#divSearchBorrower').fadeOut(200);
    }

    function FormatMe(me) {
        var val = $(me).val().replace(',', '').replace(',','').replace(',','');

        if (isNaN(parseFloat(val)))
            val = 0.00;

        var decimalFormat = parseFloat(val).toFixed(2);
        var commaFormat = FormatNumber(decimalFormat);

        $(me).val(commaFormat);
    }

    function FormatNumber(nStr) {
        nStr += '';
        var x = nStr.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }

    function ChangeAgents()
    {
        GetAgents();
        $('#hdnBorrowerID').val('');
        $('#spanBorrowerName').text('');
    }

    function GetAgents()
    {
        var val = $('#ddlBranches').val();
        var subItems = "";
        
        $.getJSON("@Url.Action("GetAgents","Loan")", { branchCode: val },
            function (data) {

                $.each(data, function (index, item) {

                    subItems += "<option value='" + $.trim(item.Value) + "'>" + $.trim(item.Text) + "</option>"

                });
                
                $("#ddlAgents").html(subItems)
            });
 
    }

    function GetNextDocumentNo()
    {
        var branchCode = $('#ddlBranches').val();
        $.getJSON("@Url.Action("GetNextLoanDocumentNo", "Loan")", { branchCode: branchCode },
            function (data) {
                $('#hdnDocumentNo').val(data);
                $('#spnDocumentNo').text(data);
            });
    }

    function SearchBorrowerList()
    {
        var branchCode = $('#ddlBranches').val();
        var searchText = $('#txtSearchText').val();
        var excludeWithActiveLoans = $('#chkExcludeWithActiveLoans').is(':checked');

        $.ajax({
            url: '/Loan/SearchBorrowers',
            type: "GET",
            dataType: "JSON",
            data: { branchCode: branchCode, searchText: searchText, excludeWithActiveLoans: excludeWithActiveLoans },
            success: function (borrowers) {

                $('#bodyBorrowers').html('');

                var rows = '';
                var append = '';

                $.each(borrowers, function (i, item) {

                    rows = '<tr><td>{borrowerName}</td><td>{agentName}</td><td><a id="lnkSelectBorrower" onclick="SelectBorrower({borrowerID}, {borrowerName2}, {agentID}, {creditLimit}); ValidateBorrower();"><span>Select</span></a></td></tr>'

                    rows = rows.replace('{borrowerName}', item.BorrowerName);
                    rows = rows.replace('{agentName}', item.AgentName);

                    rows = rows.replace('{borrowerID}', "'" + item.ID + "'");
                    rows = rows.replace('{borrowerName2}', "'" + item.BorrowerName + "'");
                    rows = rows.replace('{agentID}', "'" + item.AgentID + "'");
                    rows = rows.replace('{creditLimit}', "'" + item.CreditLimit + "'");

                    append += rows;
                });

                $("#bodyBorrowers").html(append);
            }
        });
    }
</script>

<script id="validations" type="text/javascript">

    function ValidateFields()
    {
        var okDocumentNo = ValidateDocumentNo();
        var okLoanAmount = ValidateLoanAmount();
        var okLoanInterest = ValidateLoanInterest();
        var okDocumentDate = ValidateDocumentDate();
        var okBranchCode = ValidateBranchCode();
        var okAgent = ValidateAgent();
        var okBorrower = ValidateBorrower();
        var okProceedType = ValidateProceedType();
        var okProceeds = ValidateTerm();
        //var okPaymentStartDate = ValidatePaymentStartDate();
        //var okSecondDate = ValidateSecondDate();
        var okCredit = ValidateCredit();

        if (okDocumentNo
            && okLoanAmount
            && okLoanInterest
            && okDocumentDate
            && okBranchCode
            && okAgent 
            && okBorrower
            && okProceedType
            && okProceeds 
            //&& okPaymentStartDate
            //&& okSecondDate
            && okCredit
            ) {

            $('#divValidations').hide();
            $('#btnSubmit').click();
            return true
        }
        else {
            $('#divValidations').show();
            $('html, body').scrollTop(0);
            return false;
        }
            
    }
    
    function ValidateDocumentNo()
    {
        var bln = true;
        var documentNo = $.trim($('#hdnDocumentNo').val());
        
        if (documentNo == '') {
            $('#valDocumentNo').show();
            bln = false;
        }
        else {
            $('#valDocumentNo').hide();
        }
            
        return bln;

    }

    var dateRegex = /(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;
    function ValidateDocumentDate()
    {
        var bln = true;
        var documentDate = $.trim($('#txtDocumentDate').val());
        
        if (!dateRegex.test(documentDate)) {
            $('#valDocumentDate').show();
            bln = false;
        }
        else {
            $('#valDocumentDate').hide();
        }

        return bln;
    }

    function ValidateBranchCode()
    {
        var bln = true;
        var branchCode = $.trim($('#ddlBranches').val());

        if (branchCode == '') {
            $('#valBranch').show();
            bln = false;
        }
        else {
            $('#valBranch').hide();
        }

        return bln;
    }

    function ValidateAgent()
    {
        var bln = true;
        var agent = $.trim($('#ddlAgents').val());

        if (agent == '') {
            $('#valAgent').show();
            bln = false;
        }
        else {
            $('#valAgent').hide();
        }

        return bln;
    }
    
    function ValidateBorrower()
    {
        var bln = true;
        var borrower = $.trim($('#hdnBorrowerID').val());

        if (borrower == '' || borrower == '0') {
            $('#valBorrower').show();
            bln = false;
        }
        else {
            $('#valBorrower').hide();
        }

        return bln;
        
    }

    function ValidateLoanAmount()
    {
        var bln = true;
        var loanAmount = $('#txtLoanAmount').val().replace(',', '').replace(',', '').replace(',', '');

        if ((parseFloat(loanAmount) <= 0)) {
            $('#valLoanAmount').show();
            bln = false;
        }
        else {
            $('#valLoanAmount').hide();
        }

        return bln;
    }

    function ValidateLoanInterest() {
        var bln = true;
        var loanInterest = $('#txtLoanInterest').val().replace(',', '').replace(',', '').replace(',', '');

        if ((parseFloat(loanInterest) <= 0)) {
            $('#valLoanInterest').show();
            bln = false;
        }
        else {
            $('#valLoanInterest').hide();
        }

        return bln;
    }

    function ValidateProceedType() {
        var bln = true;

        var proceedType = $.trim($('#ddlProceedType').val());

        if (proceedType == '') {
            $('#valProceedType').show();
            bln = false;
        }
        else {
            $('#valProceedType').hide();
        }

        return bln;
    }

    function ValidateTerm()
    {
        var bln = true;
        var proceeds = $.trim($('#txtTerm').val());

        if (proceeds == '' || proceeds == '0') {
            $('#valTerm').show();
            bln = false;
        }
        else {
            $('#valTerm').hide();
        }

        return bln;
        
    }

    //function ValidatePaymentStartDate() {
    //    var bln = true;
    //    var startDate = $.trim($('#txtPaymentStartDate').val());

    //    if (!dateRegex.test(startDate)) {
    //        $('#valPaymentStartDate').show();
    //        bln = false;
    //    }
    //    else {
    //        $('#valPaymentStartDate').hide();
    //    }

    //    return bln;
    //}

    //function ValidateSecondDate() {
        
    //    if ($('#ddlProceedType').val() != 3) {
    //        $('#valSecondDate').hide();
    //        return true;
    //    }

    //    var secondDate = $('#txtSecondDate').val();

    //    if ($.trim(secondDate) == '')
    //    {
    //        $('#txtSecondDate').val('0');
    //        $('#valSecondDate').show();
    //        return false;
    //    }

    //    if (isNaN(parseInt(secondDate)))
    //    {
    //        $('#txtSecondDate').val('0');
    //        $('#valSecondDate').show();
    //        return false;
    //    }

    //    if (parseInt(secondDate) == 0) {
    //        $('#txtSecondDate').val('0');
    //        $('#valSecondDate').show();
    //        return false;
    //    }
        
    //    $('#valSecondDate').hide();
    //    return true;
    //}

    //function ValidatePaymentEndDate() {
    //    var bln = true;
    //    var endDate = $.trim($('#txtPaymentEndDate').val());

    //    if (!dateRegex.test(endDate)) {
    //        $('#valPaymentEndDate').show();
    //        bln = false;
    //    }
    //    else {
    //        $('#valPaymentEndDate').hide();
    //    }

    //    return bln;
    //}

    function ValidateCredit()
    {
        var bln = true;
        var credit = $('#hdnCreditLimit').val().replace(',', '').replace(',', '').replace(',', '');
        var loanAmount = $('#txtLoanAmount').val().replace(',', '').replace(',', '').replace(',', '');

        if (parseFloat(loanAmount) > parseFloat(credit)) {
            $('#valCredit').show();
            bln = false;
        }
        else {
            $('#valCredit').hide();
        }

        return bln;
    }

</script>

}